<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <template v-if="cut">
        <el-form-item label="密码" prop="pass">
          <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
        </el-form-item>
      </template>

      <el-form-item v-if="!cut" label="年龄" prop="age">
        <el-input v-model.number="ruleForm.age" />
      </el-form-item>

      <el-form-item prop="type">
        <el-checkbox v-model="ruleForm.type" label="美食餐厅线上活动" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          提交
        </el-button>
        <el-button @click="resetForm('ruleForm')">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="btn" @click="changeBtn">
      切换
    </div>

    <hr>
    <span class="msg-text" @click="handleSend">{{ msgText }}</span>

    <div @click="goNext">
      跳转
    </div>
  </div>
</template>
<script>
export default {
  data () {
    const checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'))
        } else if (value < 18) {
          callback(new Error('必须年满18岁'))
        } else {
          callback()
        }
      }, 1000)
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      cut: true,
      ruleForm: {
        pass: '',
        checkPass: '',
        age: '',
        type: []
      },
      time: 0,

      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请至少选择一个活动性质', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    msgText () {
      return this.time > 0 ? `${this.time}s再获取` : '获取验证码'
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('执行提交!')
        } else {
          console.log('错误')
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    changeBtn () {
      this.cut = !this.cut
      console.log(this.cut, 'this.cut')
    },
    handleSend () {
      if (this.time > 0) { return }
      this.time = 60
      const timer = setInterval(() => {
        return (this.time > 0) ? this.time-- : clearInterval(timer)
      }, 1000)
    },
    goNext () {
      this.$router.push({
        path: '/live/flv',
        query: {
          flvUrl: 'https://pull.ymq01.com/liveb/392700164478873601.flv?wsSecret=6fb0f35fc2723975f238898fa4951dc3&wsABSTime=608ae034'
        }
      })
    }
  }
}
</script>
<style scoped>
.btn{
  background: #ccc;
  width: 100px;
  margin: 50px auto;
  cursor: pointer;
  text-align: center;
}
</style>
